home *** CD-ROM | disk | FTP | other *** search
- // ******************* File: prog7.C *****************
- // This program tests the general class TPVolume for
- // calculating the spline interpolation of a function
- // f(x,y,z,w)=x*y+z*w*w
- #include <TPVolume.h>
- int main()
- {
- // discrete function values on a 4D lattice:
- VecSimple(real) x(7); // lattice coordinates (in one direction)
- x(1)=-2; x(2)=-1.5; x(3)=-0.5; x(5)=0.5; x(6)=1.5; x(7)=2.0;
- VecSimplest(VecSimple(real)) xs(4); // xs is the 4D lattice coord. info
- for (int i=1; i<=4; i++) { xs(i).redim(7); xs(i)=x; } // same in all dirc.s
-
- Ptv(int) index(4); index.fill(7); // indexing in 4-dim arrays
- ArrayGenSimple(real) data(index); // function values
- int j,k,l;
- for (i=1; i<=7; i++)
- for (j=1; j<=7; j++)
- for (k=1; k<=7; k++)
- for (l=1; l<=7; l++)
- { index(1)=i; index(2)=j; index(3)=k; index(4)=l;
- data(index)=x(i)*x(j)+x(k)*sqr(x(l)); } // f(x,y,z,w)=x*y+z*w*w;
-
- // build knot vectors and spline spaces
- KnotVec knots(5); knots.fill(-2,2); knots.regulate(4);
- SplineSpace space(knots,4); // cubic spline on knots
- VecSimplest(SplineSpace) spaces(4); // same space in each direction
- for (i=1; i<=4; i++) spaces(i).redim(space);
-
- TPVolume tpv;
- tpv.redim(spaces);
- tpv.interpolation(xs,data);
- s_o<<"Interpolation in 4D is finished.\n";
-
- // evaluate the spline function at (0.66,-0.9,1.95,0.783):
- Ptv(real) p(4); p(1)=0.66; p(2)=-0.9; p(3)=1.95; p(4)=0.783;
- s_o << "spline at ("; p.print(s_o); s_o << ") = " << tpv(p);
- // ...and the derivative d^2/dzdw at the same point:
- Ptv(int) orders(4); orders(1)=0; orders(2)=0; orders(3)=1; orders(4)=1;
- real result=tpv.derivative(orders,p);
- s_o << "\nand d^2/dzdw = " << result;
- s_o << "\nExact values are " << 0.66*(-0.9)+1.95*sqr(0.783);
- s_o << " and " << 2*0.783 << '\n';
- return 0;
- }
-